package com.dcbeida.activity.salsesdedail;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.SalsesDetail2Adapter;
import com.dcbeida.adapter.SalsesDetailAdapter;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.MyClientBean;
import com.dcbeida.bean.Sales;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 退货详情
 * Created by swt on 2017/9/29.
 */

public class ReturnDetailActivity extends BaseActivity implements SalsesDetailContract.View {


    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.tv_thbh)
    TextView tvThbh;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_status)
    TextView tvStatus;
    @Bind(R.id.tv_dh)
    TextView tvDh;
    @Bind(R.id.tv_sfz)
    TextView tvSfz;
    @Bind(R.id.tv_yy_name)
    TextView tvYyName;
    @Bind(R.id.tv_box_num)
    TextView tvBoxNum;
    @Bind(R.id.tv_money)
    TextView tvMoney;
    @Bind(R.id.myList)
    ListViewForScrollView myList;
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.tv_thyp)
    TextView tvThyp;
    private ArrayList<Object> datalist;
    private SalsesDetail2Adapter adapter;
    private SalsesDetailPresenter mPresent;
    private Sales.ListBean salesBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.return_detail;
    }

    @Override
    public void setData() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            salesBean = (Sales.ListBean) bundle.getSerializable("Sales.ListBean");
        }
        tvThyp.setVisibility(View.GONE);
        imgBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.thxq);
        tvThbh.setText(salesBean.getId());
        tvTime.setText(getString(R.string.thsj) + salesBean.getCreatetime());
        if (salesBean.getStatus().equals("0")) {
            //审核中
            tvThyp.setText(R.string.sqthyp);
            tvStatus.setText(R.string.shz);
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_1AAD19));
        } else {
            //退款成功
            tvThyp.setText(R.string.ythyp);
            tvStatus.setText(R.string.tkcg);
            tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_999999));
        }
        tvName.setText(salesBean.getTruename());
        tvDh.setText(getString(R.string.dh) + salesBean.getPhonenum());
        tvBoxNum.setText("-" + salesBean.getQuantity());
        tvSfz.setText(getString(R.string.sfzh) + salesBean.getIdcardnum());
        tvYyName.setText(salesBean.getHospitalname());
        tvMoney.setText("-" + getString(R.string.yuan) + salesBean.getAmount());

        scrollView.smoothScrollTo(0, 0);
        datalist = new ArrayList<Object>();
        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new SalsesDetail2Adapter(this);
        myList.setAdapter(adapter);

        mPresent = new SalsesDetailPresenter(this, this);
        mPresent.getSalsesDetailInfo(MyApplication.user.getUid(), salesBean.getId(), "0");
    }


    @OnClick({R.id.img_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }


    @Override
    public void showTip(String msg) {

    }

    @Override
    public void showSalsesDetailList(ArrayList<SalsesDetail.ListBean> list) {
        ArrayList<SalsesDetail.ListBean> tempList = new ArrayList<>();

        DrugListBean listBean = MyApplication.getInstance().getmDrugBean();
        if (listBean == null || listBean.getList() == null)
            return;


        for (Drug drug : listBean.getList()) {
            List<SalsesDetail.ListBean> drugList = new ArrayList<>();
            int count = 0;
            for (SalsesDetail.ListBean bean : list) {
                if (!bean.getRefundid().equals("0")) {
                    if (drug.getDrugid() == bean.getDrugid()) {
                        count += Integer.parseInt(bean.getQuantity());
                        bean.setDrugName(drug.getDrugname());
                        drugList.add(bean);
                    }

                }
            }
            if (drugList.size() > 0) {
                SalsesDetail.ListBean data = drugList.get(0);
                data.setFirst(true);
                data.setCount(count);
            }
            tempList.addAll(drugList);
            //tpdo
        }


        adapter.setmArrayList(tempList);
        //todo
//        if (adapter.getCount() > 0) {
//            tvThyp.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void showList(ArrayList<MyClientBean> list) {

    }

    @Override
    public void showSignStatus(int signStatus) {

    }


    @Override
    public void setPresenter(SalsesDetailContract.Present presenter) {

    }
}
